import request from "./useRequests";
import { userinfo } from '@/stores/counter'
import getUser from '@/api/getUser'
import { ElMessage } from 'element-plus'

export default function getDiscussionsByIds() {
    const user = userinfo()
    const getName = getUser()
    async function getDiscussionsInCate(discussionCateId: number, page: number = 1, limit: number = 9999) {
        try {
            const res = await request({
                method: "get",
                url: `/news/getDiscussionsByIds`,
                params: {
                    discussionCateId,
                    page,
                    limit,
                }
            })
            let data = res.data.data.list
            for (let i = 0; i < data.length; i++) {
                data[i].nickName = await getName.getName(data[i].userId)
            }
            return data

        } catch (err: any) {
            //alert(err)
        }
    }
    async function getDiscussionsInUser(discussionCateId: number, userId: number, page: number = 1, limit: number = 9999) {
        try {
            const res = await request({
                method: "get",
                url: `/news/getDiscussionsByIds`,
                params: {
                    discussionCateId,
                    userId,
                    page,
                    limit,
                }
            })
            let data = res.data.data.list
            for (let i = 0; i < data.length; i++) {
                data[i].nickName = await getName.getName(data[i].userId)
            }
            return data

        } catch (err: any) {
            //alert(err)
        }
    }

    return {
        getDiscussionsInCate, getDiscussionsInUser
    }
}